<?php

namespace app\mobile\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;

class Xiangsu extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
		//轮播图
		$banner_type_id=Db::name('bannertype')->where(['type'=>1,'name'=>'橡塑装备'])->column('id');

		$rs_banner=Db::name('banner')->where([
			'type_data'=>1,
			'bannertype_id'=>$banner_type_id[0],
			'endtime'=>['>',time()],
		])->order('weigh desc')->select();
		$this->assign('rs_banner',$rs_banner);

		$nav=Db::name('category')->where('type','product')
            ->where('pid',0)
            ->where('status','normal')
            ->order('weigh desc')->limit(7)
            ->select();
		$this->assign('nav',$nav);

		$qiye=Db::name('qiye')->where(['h5_switch'=>1,'name'=>['<>','']])->limit(4)->order('createtime desc')->select();
		/*$qiye_ids=[];
		foreach($qiye1 as $k=>$v){
			$qiye_ids[]=$v['id'];
		}

		$qiye2=Db::name('qiye')->where([
			'id'=>['not in',$qiye_ids]
		])->orderRaw('rand()')->limit(2)->select();

		$qiye = array_merge($qiye1, $qiye2);*/
		$this->assign('qiye',$qiye);

		$product=Db::name('product')->alias('p')->join('qiye q','p.qiye_id=q.id','LEFT')->field('p.*,q.name as qiye_name')->where(['p.status'=>'2'])->order('weigh desc')->paginate(10);

		$this->assign('product',$product);
        return $this->view->fetch();
    }

	public function search(Request $request){
		$keyword=$request->param('keyword/s','');
		$rs_qiye=Db::name('qiye')->where(['name'=>['like',"%$keyword%"]])->select();
		$rs_product=Db::name('product')->alias('p')->where(['p.name'=>['like',"%$keyword%"]])->field('p.*,q.name as qiye_name')
		->join('qiye q','p.qiye_id=q.id','LEFT')
		->select();

		$this->assign('keyword',$keyword);
		$this->assign('rs_qiye',$rs_qiye);
		$this->assign('rs_product',$rs_product);
		$this->assign('empty','<div class="empty">
					<div>
						<img src="/mobile/img/ico_empty.png" >
						<p>没有查询到对应内容</p>
					</div>
				</div>');
		return $this->view->fetch();
	}

	public function all_category(){
		$category=Db::name('category')->where(['type'=>'product','pid'=>0,'status'=>'normal'])->select();
		foreach($category as $k=>$v){
			$children=Db::name('category')->where(['type'=>'product','pid'=>$v['id'],'status'=>'normal'])->select();
			foreach($children as $m=>$n){
				$children[$m]['level3']=Db::name('category')->where(['type'=>'product','pid'=>$n['id'],'status'=>'normal'])->select();
			}
			$category[$k]['level2']=$children;
		}
		$this->assign('category',$category);
		return $this->view->fetch();
	}

	public function category(Request $request){
		$id=$request->param('id/d',0);


		$category=Db::name('category')->where('id',$id)->find();
		$parent_category=Db::name('category')->where('id',$category['pid'])->where('status','normal')->find();
		$brother=Db::name('category')->where('pid',$parent_category['id'])->where('status','normal')->select();

		$children_ids=getChildIds($id);

		$product=Db::name('product')->alias('p')
		->join('qiye q','p.qiye_id=q.id','LEFT')->field('p.*,q.name as qiye_name')
		->where(['p.status'=>'2','p.category_id'=>['in',$children_ids]])->order('weigh desc,updatetime desc')->paginate(20);
        //var_dump($product);exit;
		$this->assign('rs',$product);

		$this->assign('empty','<div class="empty">
					<div>
						<img src="/mobile/img/ico_empty.png" >
						<p>没有查询到对应内容</p>
					</div>
				</div>');
		return $this->view->fetch();
	}
}
